package com.Controller;

import com.POJO.Engineer;
import com.POJO.User;
import com.Service.EngineerService;
import com.Service.UserService;
import com.Utils.EngineerRegistrationDTO;
import com.Utils.JWTUtil;
import com.Utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@CrossOrigin
@RestController
@RequestMapping("/Engineer")
public class EngineerController {
    @Autowired
    private EngineerService engineerService;

    @PostMapping("/findTel")
    public R findTel(@RequestBody Map<String,String> Map_All){
        return new R(engineerService.EngineerFindTel(Map_All));
    }

    @PostMapping("/Register")
    public R Register(@RequestBody EngineerRegistrationDTO registrationDTO){
        String tel=registrationDTO.getTel();
        String pwd=registrationDTO.getPwd();
        List<String> subjectList=registrationDTO.getSubjectList();
        return new R(engineerService.EngineerRegister(tel,pwd,subjectList)>0);
    }
    @PostMapping("/Login")
    public R Login(@RequestBody Map<String,String> Map_All){
        String tel=Map_All.get("tel");
        String pwd=Map_All.get("pwd");
        Engineer engineer=engineerService.Login(tel,pwd);
        if (engineer == null) {
            //登录失败
            return new R(false, "用户名或密码不正确");
        } else {
            //登录成功
            Map<String, String> dataMap = new HashMap<>();
            dataMap.put("tel", tel);
            Object token = JWTUtil.getToken(dataMap);
            Map<String,Object> data=new HashMap<>();
            data.put("username",engineer.getUsername());
            data.put("tel",engineer.getTel());
            data.put("token",token);
            return new R(true, data, "登录成功");
        }
    }
    @PostMapping("/FindEngineer")
    public R FindUser(@RequestBody Map<String,String> Map_All){
        String tel =Map_All.get("tel");
        return new R(true,engineerService.FindEngineer(tel));
    }
    @PostMapping("/EngineeraddAddress")
    public R EngineeraddAddress(@RequestBody Map<String,String> Map_All){
        return new R(true,engineerService.EngineeraddAddress(Map_All));
    }
    @PostMapping("/updataHeadshot")
    public R updataHeadshot(@RequestParam("tel") String tel,@RequestPart("file") MultipartFile headshot){
        return new R(engineerService.updataHeadshot(tel,headshot)>0);
    }
    @PostMapping("/updataUsername")
    public R updataUsername(@RequestBody Map<String,String> Map_All){
        String tel=Map_All.get("tel");
        String username=Map_All.get("username");
        return new R(engineerService.updataUsername(tel,username)>0);
    }
    @PostMapping("/updataPwd")
    public R updataPwd(@RequestBody Map<String,String> Map_All){
        String tel=Map_All.get("tel");
        String pwd=Map_All.get("pwd");
        return new R(engineerService.updataPwd(tel,pwd)>0);
    }
}
